package com.zoweunion.mechanic.controller;


import com.zoweunion.mechanic.model.ResponseBean;
import com.zoweunion.mechanic.service.ChannelPolicyService;
import com.zoweunion.mechanic.util.MyException;
import com.zoweunion.mechanic.util.constants.Constants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @description 渠道商政策管理
 */
@RestController
@RequestMapping("/channelPolicy")
public class ChannelPolicyController extends BaseController {
    @Autowired ChannelPolicyService channelPolicyService;

    @PostMapping("/add")
    public ResponseBean addChannelPolicy(@RequestBody Map<String, Object> params, HttpServletRequest request) throws MyException {
        String user_id = getCurrentUser(request).getId();
        String s_id = getCurrentUser(request).getS_id();
        params.put("user_id", user_id);
        params.put("s_id", s_id);
        int result = channelPolicyService.addChannelPolicy(params);
        if(result > 0) {
            return new ResponseBean(Constants.CODE_SUCCESS, "新增政策成功！", null);
        }
        return new ResponseBean(Constants.CODE_ERROR, "新增政策失败！", null);
    }

    @PostMapping("/edit")
    public ResponseBean editChannelPolicy(@RequestBody List<Map<String, Object>> list, HttpServletRequest request) throws MyException {
        String user_id = getCurrentUser(request).getId();
        String s_id = getCurrentUser(request).getS_id();
//        List<Map<String, Object>> list = (List<Map<String, Object>>)params.get("list");
        list.forEach(item->{
            item.put("user_id", user_id);
            item.put("s_id", s_id);
        });
        int result = channelPolicyService.editChannelPolicy(list);
        if(result > 0) {
            return new ResponseBean(Constants.CODE_SUCCESS, "更新政策成功！", null);
        }
        return new ResponseBean(Constants.CODE_ERROR, "更新政策失败！", null);
    }

    @GetMapping("/selectChannelPolicyListBySid")
    public ResponseBean selectChannelPolicyListBySid(HttpServletRequest request) throws MyException {
        String s_id = getCurrentUser(request).getS_id();
        Map<String, Object> params = new HashMap<>();
        params.put("s_id", s_id);
        List<Map<String, Object>> list = channelPolicyService.selectChannelPolicyListBySid(params);
        return new ResponseBean(Constants.CODE_SUCCESS, "查询政策成功！", list);
    }

    @PostMapping("/deleteChannelPolicyById")
    public ResponseBean deleteChannelPolicyById(@RequestBody Map<String, Object> params, HttpServletRequest request) throws MyException {
        String s_id = getCurrentUser(request).getS_id();
        params.put("s_id", s_id);
        int result = channelPolicyService.deleteChannelPolicyById(params);
        if(result > 0) {
            return new ResponseBean(Constants.CODE_SUCCESS, "删除政策成功！", null);
        }
        return new ResponseBean(Constants.CODE_ERROR, "删除政策失败！", null);
    }
}
